package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.vo.BrandVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 查询该品牌关联的分类
     * 用的是pms_category分类表    pms_brand品牌表   pms_catelog_brand_relation他俩的中间表
     * 看开发文档,前端传brandId他要catelogId和catelogName,恰好,这两个字段在我们中间表都有
     */
    @GetMapping("/catelog/list")
//    @RequiresPermissions("product:categorybrandrelation:list")
    public R cateloglist(@RequestParam("brandId") Long brandId) {
        System.out.println("brandId:" + brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
        );
        System.out.println("data:" + data);
        // 就是根据当前传来的品牌id,注意,这个data里面不仅包含了catelogId和catelogName还包含了brandName和brandId,都在中间表里,他开发文档有问题,不可全信
        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 获取分类关联的品牌
     * /product/categorybrandrelation/brands/list
     * 请求参数:catId:三级分类的id
     * 响应数据:brandId:品牌的id  brandName
     */
    @GetMapping("/brands/list")
    public R relationBrandList(@RequestParam(value = "catId", required = true) Long catId) {
        List<BrandEntity> vos = categoryBrandRelationService.getBrandByCatId(catId);
        List<BrandVO> collect = vos.stream().map(item -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(item.getBrandId());
            brandVO.setBrandName(item.getName());
            return brandVO;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    /**
     * 给品牌新增一个关联分类(分类即三级分类)
     * 用的是pms_category分类表    pms_brand品牌表   pms_catelog_brand_relation他俩的中间表
     * 看开发文档,前端传的是brandId和catalogId,要的是msg:"success"和code:0
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        System.out.println("categoryBrandRelation:" + categoryBrandRelation);
        /**
         * 打印出的信息:
         * categoryBrandRelation:CategoryBrandRelationEntity(id=null, brandId=4, catelogId=229, brandName=null, catelogName=null)
         * brandId=4是华为,catelogId=229是装宽带,但是brandName和catelogName并没有
         * 因此categoryBrandRelationService.saveDetail(categoryBrandRelation);就是给但是brandName和catelogName赋值
         * 怎么操作看这个service的实现类
         */
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        // ids:[4]是pms_category_brand_relation中间表的id,关联中剔除只删除中间表的数据
        System.out.println("ids:" + Arrays.toString(ids));
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
